package gemini.castor.ui.server.service.impl;

import gemini.basic.manager.ShoppingManager;
import gemini.basic.model.Cart;
import gemini.basic.model.Order;
import gemini.basic.model.Payment;
import gemini.basic.model.Person;
import gemini.castor.ui.client.service.ShoppingService;
import gemini.castor.ui.server.service.AbstractSingleService;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class ShoppingServiceImpl extends
        AbstractSingleService<ShoppingManager> implements
        ShoppingService {

    @SuppressWarnings("unused")
	private final Log logger = LogFactory
            .getLog(ShoppingServiceImpl.class);

	@Override
	public Cart createCart(Person person) {
		Cart result = duplicate(getServerManager().createCart(person));
    	return result;
	}

	@Override
	public Order saveOrUpdateOrder(Order order) {
		Order result;
		if(order.getId() == null){
			result = duplicate(getServerManager().saveOrder(order));
		} else {
			result = duplicate(getServerManager().updateOrder(order));
		}
    	return result;
	}

	@Override
	public void deleteOrder(Order order) {
		getServerManager().deleteOrder(order);
	}

	@Override
	public Cart getOpenCartByDistributorId(int distributorId) {
		Cart result = duplicateExcludeFields(getServerManager().getOpenCartByDistributorId(distributorId));
    	return result;
	}

	@Override
	public ArrayList<Order> saveOrUpdateOrders(ArrayList<Order> orders) {
		List<Order> resultList = getServerManager().saveOrUpdateOrders(orders);
		ArrayList<Order> result = null;
		
		if(resultList != null){
			result = new ArrayList<Order>(resultList.size());
			for (Order or : resultList) {
				result.add(duplicate(or));
			}
		}
		return result;
	}

	@Override
	public void deleteOrders(ArrayList<Order> orders) {
		getServerManager().deleteOrders(orders);
	}

	@Override
	public void completeCart(int cartId, Payment payment) {
		getServerManager().completeCart(cartId, payment);
	}

	@Override
	public void checkOut(Payment payment) {
		getServerManager().checkOut(payment);
	}

}
